package com.yeung.dao;

import com.yeung.beans.Department;
import com.yeung.beans.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 模拟接入了数据库获取数据
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<>();
        Department ad = new Department(101, "D-AA");
        Department bd = new Department(102, "D-BB");
        Employee e1 = new Employee(1001, "E-AA", "aa@163.com", "男");
        e1.setDepartment(ad);
        Employee e2 = new Employee(1002, "E-BB", "BB@163.com", "男");
        e2.setDepartment(bd);
        employees.put(1001, e1);

        employees.put(1002, e2);
    }

    private int inidId = 1;

    public Collection<Employee> getEmployess() {
        return employees.values();
    }

    public Employee getEmployeeById(int id) {
        return employees.get(id);
    }

    public void delete(int id) {
        employees.remove(id);
    }

    public void save(Employee employee) {
        if (employee.getId() == 0)
            employee.setId(inidId++);
        employees.put(employee.getId(), employee);
    }
}
